﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace STX.Lib.Web
{
    /// <summary>
    /// 
    /// </summary>
    public class Cookie
    {
        private Cookie() { }

        private static Cookie _instance = new Cookie();

        public static Cookie Instance
        {
            get { return _instance; }
            set { _instance = value; }
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Set(string key, string value, bool httpOnly = false)
        {
            Set(key, value, "/", "", 0, httpOnly);
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="domain">二级域名时定位</param>
        /// <param name="hour"></param>
        public void Set(string key, string value, string path, string domain, int hour, bool httpOnly)
        {
            Set(key, value, path, domain, DateTime.Now.AddHours(hour), httpOnly);
        }

        /// <summary>
        /// SetCookie
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="path"></param>
        /// <param name="domain">二级域名时定位</param>
        /// <param name="expires"></param>
        public void Set(string key, string value, string path, string domain, DateTime expires,bool httpOnly)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            bool isAdd = false;
            if (cookie == null)
            {
                isAdd = true;
                cookie = new HttpCookie(key);
            }
            if (httpOnly)
            {
                cookie.HttpOnly = true;
            }
            cookie.Path = path;
            cookie.Domain = domain;
            cookie.Value = HttpUtility.UrlEncode(value);
            if (expires > DateTime.Now)
            {
                cookie.Expires = expires;
            }

            if (isAdd)
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }

        /// <summary>
        /// Cookie
        /// </summary>
        public string Get(string key)
        {
            var cookie = HttpContext.Current.Request.Cookies[key];
            return cookie != null ? HttpUtility.UrlDecode(cookie.Value) : "";
        }
        /// <summary>
        /// Cookie
        /// </summary>
        /// <param name="key"></param>
        public void Remove(string key)
        {
            Remove(key, "", "");
        }

        public void Remove(string key, string path, string domain)
        {
            var hc = new HttpCookie(key);
            if (path != "")
            {
                hc.Path = path;
            }
            if (domain != "")
            {
                hc.Domain = domain;
            }
            hc.Expires = DateTime.Now.AddDays(-1);

            HttpContext.Current.Response.Cookies.Add(hc);
        }

    }
}
